package com.gperp.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gperp.common.GPResult;
import com.gperp.mapper.AsKeywordsMapper;
import com.gperp.mapper.AsSystemconfigMapper;
import com.gperp.pojo.AsKeywords;
import com.gperp.pojo.AsKeywordsExample;
import com.gperp.pojo.AsSystemconfigExample;
import com.gperp.pojo.Result;
import com.gperp.service.AsKeywordsService;

@Service
public class AsKeywordsServiceImpl implements AsKeywordsService {

	@Resource
	private AsKeywordsMapper asKeywordsMapper;
	
	@Resource
	private AsSystemconfigMapper asSystemconfigMapper;
	
	@Override
	public Result getlikeKeywords(int page, int rows,String keywords) {
		
		// 设置分页信息
		PageHelper.startPage(page, rows);
		// 执行查询
		AsKeywordsExample example=new AsKeywordsExample();
		if (keywords!=null && keywords.length()>0) {
			example.createCriteria().andKeywordsLike("%"+keywords+"%");
		}
		
		List<AsKeywords> list = asKeywordsMapper.selectByExample(example);
		
		// 取分页结果
		PageInfo<AsKeywords> pageInfo = new PageInfo<>(list);
		Result r = new Result();
		r.setRows(pageInfo.getList());
		r.setPageIndex(page);
		r.setTotal(pageInfo.getTotal());		
		return r;
	}

	@Override
	public GPResult checkCode(String data) {
		if(data!=null&&!data.equals("")){
			try {
				String[] da = data.split(",");
				AsKeywords key = new AsKeywords();
				key.setId(Long.parseLong(da[1]));
				switch (Integer.parseInt(da[0])) {
				case 1:
					key.setIsuse(1);
					break;
				case 2:
					key.setIsuse(0);
					break;
				case 3:
					key.setCheckstatus(1);
					break;
				case 4:
					key.setCheckstatus(2);
					break;
				case 5:
					key.setCheckstatus(3);
					break;
				default:
					break;
				}
				asKeywordsMapper.updateByPrimaryKeySelective(key);
			} catch (NumberFormatException e) {
				e.printStackTrace();
				return GPResult.build(200, "NO");
			}
		}else{
			return GPResult.build(200, "NO");
		}
		return GPResult.ok();
	}

	@Override
	public List selectkeywordsmoney(Long id) {
		AsSystemconfigExample example=new AsSystemconfigExample();
		example.createCriteria().andConfigtypeEqualTo(2);
		AsSystemconfigExample example1=new AsSystemconfigExample();
		example1.createCriteria().andConfigtypeEqualTo(3);
		List list=new ArrayList();
		list.add(asKeywordsMapper.selectByPrimaryKey(id));
		list.add(asSystemconfigMapper.selectByExample(example));
		list.add(asSystemconfigMapper.selectByExample(example1));
		return list;
	}

	@Override
	public List selecttypeyear() {
		AsSystemconfigExample example=new AsSystemconfigExample();
		example.createCriteria().andConfigtypeEqualTo(2);
		AsSystemconfigExample example1=new AsSystemconfigExample();
		example1.createCriteria().andConfigtypeEqualTo(3);
		List list=new ArrayList();
		list.add(asSystemconfigMapper.selectByExample(example));
		list.add(asSystemconfigMapper.selectByExample(example1));
		return list;
	}

}
